local module = {}
module.name = "Timer"
module.Init = function()
	if not SCDB.modules[module.name] then return end
	if SCDB[module.name] == nil then SCDB[module.name] = {} end

	TimerTracker:UnregisterAllEvents()
	
	local tt = CreateFrame("Frame", "TimersAnchor", UIParent)
	tt:SetSize(150, 18)
	tt:SetPoint("CENTER")
	tt:SetClampedToScreen(true)
	if UIMovableFrames then tinsert(UIMovableFrames, tt) end
	
	tt.timerList = {}
	tt:RegisterEvent("START_TIMER")
	tt:RegisterEvent("PLAYER_ENTERING_WORLD")
	tt:SetScript("OnEvent", function(self, event, ...)
		if event == "START_TIMER" then
			local timerType, timeSeconds, totalTime  = ...
			local timer
			local numTimers = 0
			local isTimerRuning = false
			totalTime = totalTime + 5
			
			for a, b in pairs(self.timerList) do
				if b.type == timerType and not b.isFree then
					timer = b
					isTimerRuning = true
					break
				end
			end
			
			if not isTimerRuning then
				for a, b in pairs(self.timerList) do
					if not timer and b.isFree then
						timer = b
					else
						numTimers = numTimers + 1
					end
				end
				
				if not timer then
					timer = CreateFrame("StatusBar", "MTimer"..(#self.timerList+1), self)
					self.timerList[#self.timerList+1] = timer
					
					timer:SetWidth(150)
					timer:SetHeight(18)
					timer:SetStatusBarTexture("Interface\\Addons\\"..addonName.."\\media\\texture\\statusbar")
					timer:SetStatusBarColor(1, 0, 0)
					timer.bg = timer:CreateTexture(nil, "BACKGROUND")
					timer.bg:SetTexture(0,0,0)
					timer.bg:SetPoint("TOPLEFT", -1, 1)
					timer.bg:SetPoint("BOTTOMRIGHT", 1, -1)
					timer.label = timer:CreateFontString(timer:GetName().."Text", "ARTWORK")
					timer.label:SetPoint("LEFT", 3, 0)
					timer.label:SetPoint("RIGHT", -3, 0)
					timer.label:SetFontObject("GameFontNormal")
					timer.label:SetJustifyH("CENTER")
				end
				
				timer:ClearAllPoints()
				timer:SetPoint("TOP", 0, 100)
				timer:SetAlpha(1)
				
				timer.isFree = false
				timer.type = timerType
				timer.time = timeSeconds
				timer:SetMinMaxValues(0, totalTime)
				
				timer.updateTime = TIMER_UPDATE_INTERVAL
				timer:SetScript("OnUpdate", function(self, elasped)
					self.time = self.time - elasped
					self.updateTime = self.updateTime - elasped
					local minutes, seconds = floor(self.time/60), floor(mod(self.time, 60))

					if self.updateTime <= 0 then
						ValidateTimer(self.type)
						self.updateTime = TIMER_UPDATE_INTERVAL
					end
					
					if seconds < 0 then
						self:SetAlpha(0)
					end

					self:SetValue(self.time)
					self.label:SetText(string.format(TIMER_MINUTES_DISPLAY, minutes, seconds))
				end)
			end
		elseif event == "PLAYER_ENTERING_WORLD" then
			for a,timer in pairs(self.timerList) do
				timer.time = nil
				timer.type = nil
				timer.isFree = nil
				timer:SetScript("OnUpdate", nil)
				timer:SetAlpha(0)
			end
		end
	end)

end
tinsert(SuperClassic.modules, module) -- finish him!